﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using Microsoft.Practices.ServiceLocation;
using System.Windows.Controls;
using System.Xml.Linq;
using System.IO;

namespace Dynamic_UI_Framework
{
    [Export(typeof(UITree))]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public class UITree
    {
        public UITree()
        {
            BenchAdapterMapping mappint = ServiceLocator.Current.GetInstance<BenchAdapterMapping>() as BenchAdapterMapping;
            IAdapter adapter = mappint.GetMapping(typeof(UserControl));
            Root = adapter.DefaultBuild("root");

            Load();
        }

        public IVisual Root { get; private set; }

        public TreeViewItem GetTreeViewItem()
        {
            IBuilder builder = ServiceLocator.Current.GetInstance<IBuilder>(Root.GetType().Name);
            UINode node = builder.GetUINode(Root);

            TreeViewItem item = new TreeViewItem();
            item.Header = node.TypeName;
            item.Tag = node;
            item.ContextMenu = new ContextMenu();
            foreach (var mi in node.Target.GetMenuItem())
            {
                item.ContextMenu.Items.Add(mi);
            }
            if (node.Target is IRegion == false)
            {
                foreach (var n in node.Children)
                {
                    DoGetTreeViewItem(item, n);
                }
            }
            return item;
        }

        private void DoGetTreeViewItem(TreeViewItem item, UINode node)
        {
            TreeViewItem tvi = new TreeViewItem { Header = node.TypeName };
            tvi.Tag = node;
            tvi.ContextMenu = new ContextMenu();
            foreach (var menu in node.Target.GetMenuItem())
            {
                tvi.ContextMenu.Items.Add(menu);
            }
            if (node.Target is IRegion == false)
            {
                foreach (var n in node.Children)
                {
                    DoGetTreeViewItem(tvi, n);
                }
            }
            item.Items.Add(tvi);
        }

        public void Save()
        {
            IBuilder builder = ServiceLocator.Current.GetInstance<IBuilder>(Root.GetType().Name);
            if (builder != null)
            {
                UINode node = builder.GetUINode(this.Root);
                XElement ele = node.WriteXml();
                ele.Save(UISetting.UITreePath);
            }
        }

        public void Load()
        {
            if (File.Exists(UISetting.UITreePath) == false)
                return;

            XElement ele = XElement.Load(UISetting.UITreePath);
            UINode node = new UINode();
            node.LoadXml(ele);
            IBuilder builder = ServiceLocator.Current.GetInstance<IBuilder>(node.TypeName);
            IVisual v = builder.Build(node);
            this.Root = v;
        }
    }
}
